﻿/**********************************************
系统:Ebada物流企业ERP
模块:示例
作者:Rabbit
创建时间:2011-2-28
最后一次修改:2011-2-28
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using DevExpress.XtraGrid.Columns;
using System.Reflection;
using Ebada.Client;
using DevExpress.XtraGrid.Views.Base;
using Ebada.Client.Platform;

namespace Ebada.Modules.Document
{

    public partial class UCDocumentsReject : DevExpress.XtraEditors.XtraUserControl {
        private GridViewOperation<Documents> gridViewOperation;

        public event SendDataEventHandler<Documents> FocusedRowChanged;
        public static bool FirstLoad = false;
        public UCDocumentsReject()
        {
            InitializeComponent();
           
           // initColumns();
            gridViewOperation = new GridViewOperation<Documents>(gridControl1, gridView1, barManager1);
            gridViewOperation.CreatingObjectEvent +=gridViewOperation_CreatingObjectEvent;
            gridView1.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gridViewOperation_FocusedRowChanged);
            Add_columnName();
            Init_Org();
        }
        public void HideColums()
        {
            // gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocCode"].Visible = false;
            gridView1.Columns["DocumentTypeID"].Visible = false;
            gridView1.Columns["DocumentPositionID"].Visible = false;
        }
        void gridViewOperation_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) 
        {
            if (FocusedRowChanged != null)
                FocusedRowChanged(gridView1, gridView1.GetFocusedRow() as Documents);
            Documents row = gridView1.GetFocusedRow() as Documents;
            if (row != null)
            {
                if (row.DocState == 3)
                {
                   
                    this.barButtonItem1.Enabled = false;
                }
                else
                {
                   
                    this.barButtonItem1.Enabled = true;
                }
            }
        }

        //将关联单位标识改为关联单位
        //并以中文显示
        public void Init_Org()
        {
            gridView1.Columns["Org_ID"].Caption = "关联单位";
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["Org_ID"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            comboBox.DataSource = org_list;
            comboBox.DisplayMember = "cnName";
            comboBox.ValueMember = "Org_ID";

        }
        public void Add_columnName()
        {
            gridView1.Columns["DocumentTypeName"].Caption = "单证种类";
            gridView1.Columns["DocState"].Caption = "单证状态";

            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit comboBox = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            gridView1.Columns["DocState"].ColumnEdit = comboBox;
            IList<mOrg> org_list = MainHelper.PlatformSqlMap.GetList<mOrg>("");
            DataTable temptable = new DataTable();
            temptable.Columns.Add("code", typeof(int));
            temptable.Columns.Add("Name", typeof(string));
            DataRow row1 = temptable.NewRow();
            DataRow row2 = temptable.NewRow();
            DataRow row3 = temptable.NewRow();
            DataRow row4 = temptable.NewRow();
            row1["code"] = 0;
            row1["Name"] = "未提交";

            row2["code"] = 1;
            row2["Name"] = "已提交";

            row3["code"] = 2;
            row3["Name"] = "借出";

            row4["code"] = 3;
            row4["Name"] = "报废";
            temptable.Rows.Add(row1);
            temptable.Rows.Add(row2);
            temptable.Rows.Add(row3);
            temptable.Rows.Add(row4);


            comboBox.DataSource = temptable;
            comboBox.DisplayMember = "Name";
            comboBox.ValueMember = "code";
        }
        /// <summary>
        /// 设置隐藏列
        /// </summary>
       public  void initColumns() 
        {
            //隐藏单证前5列
            //for (int i = 0; i < 5; i++)
            //{
            //    gridView1.Columns[i].Visible = false;
            //}
            gridView1.Columns["OrgName"].Caption = "责任单位名称";
            gridView1.Columns["DocState"].Caption = "单证状态";
            gridView1.Columns["DocState"].Visible = false;
            gridView1.Columns["DocStateName"].Caption = "单证状态";
            gridView1.Columns["DocCode"].Caption = "单证编号";
            gridView1.Columns["Act_Date"].Caption = "作业时间";
            gridView1.Columns["SubmitDateLimit"].Caption = "单证提交时限";
            gridView1.Columns["SubmitDate"].Caption = "实际提交时间";
            gridView1.Columns["ValidityDate"].Caption = "有效截至期";
            gridView1.Columns["RemindDate"].Caption = "有效期预警时间";
            gridView1.Columns["CheckResult"].Caption = "审查结果";
            gridView1.Columns["CheckRemark"].Caption = "审查说明";
            gridView1.Columns["CheckUserID"].Caption = "审查人ID";
            gridView1.Columns["CheckUserName"].Caption = "审查人";
            gridView1.Columns["CheckedDate"].Caption = "审查时间";
            gridView1.Columns["Org_ID"].Caption = "承运商";
            gridView1.Columns["SpliteFlag"].Visible = false;
            gridView1.Columns["ScrapDate"].Caption = "报废时间";
            gridView1.Columns["DocBookID"].Caption = "册子编号";
            gridView1.Columns["DocBookName"].Caption = "册子名称";
            gridView1.Columns["DocBookSeq"].Caption = "册子里序列号";
            gridView1.Columns["CreateUserID"].Caption = "创建人ID";
            gridView1.Columns["LastEditUserID"].Caption = "修改人ID";
           
        }
       public void Hide_Find()
       {
           this.btFind.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           this.btAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           
       }
        /// <summary>
        /// 封装了数据操作的对象
        /// </summary>
        [Browsable(false)]
        public GridViewOperation<Documents> GridViewOperation {
            get { return gridViewOperation; }
            set { gridViewOperation = value; }
        }
        /// <summary>
        /// 新建对象设置Key值
        /// </summary>
        /// <param name="newobj"></param>
        void gridViewOperation_CreatingObjectEvent(Documents newobj) {
            newobj.DocumentID = Guid.NewGuid().ToString();
           
        }
        /// <summary>
        /// 父表ID
        /// </summary>
        [Browsable(false)]
       //根据库位信息刷新单证
        public void RefreshData(List<string> str_list)
        {

            string str = "where 2>1";
            if (str_list.Count>0)
            {
                str = "where DocumentPositionID in (";
                for (int i = 0; i < str_list.Count; i++)
                {
                    if (i == 0)
                    {
                        str += "'" + str_list[i] + "'";
                    }
                    else
                    {
                        str += "," + "'" + str_list[i] + "'";
                    }
                   
                }
                str += ") and ValidityDate<'" + DateTime.Now + "' order by DocState";
               // string str2 = " or DocumentPositionID=";
                //str = str.Substring(0, str.Length - str2.Length);
            }
            gridViewOperation.RefreshData(str);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.Site == null)
                InitData();
        }
        public void InitData()
        {
            gridViewOperation.RefreshData("Where DocumentID!=''order by DocState");
            HideColums();
            if (FirstLoad)
            {
                gridViewOperation.RefreshData("Where DocState=1");
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Documents ds = gridView1.GetFocusedRow() as Documents;
            if (ds.DocState==3)
            {
                MessageBox.Show("已经报废！");
                return;
            }
            if (MessageBox.Show("确定要报废吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
              
                ds.DocState = 3;
                ds.ScrapDate = DateTime.Now;
                MainHelper.PlatformSqlMap.Update<Documents>(ds);
            }
           
        }
        private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                string type = view.GetRowCellDisplayText(e.RowHandle, view.Columns["DocState"]);
                //row1["code"] = 0;
                //row1["Name"] = "未提交";

                //row2["code"] = 1;
                //row2["Name"] = "已提交";

                //row3["code"] = 2;
                //row3["Name"] = "借出";

                //row4["code"] = 3;
                //row4["Name"] = "报废";
                if (type == "未提交")
                {
                    //e.Appearance.BackColor = Color.White;
                }
                if (type == "已提交")
                {
                    e.Appearance.BackColor = Color.LightSkyBlue;
                }
                if (type == "借出")
                {
                    e.Appearance.BackColor = Color.LightYellow;
                }
                if (type == "报废")
                {
                    e.Appearance.BackColor = Color.LightGray;
                }
            }

        }
    }
}
